/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-16
 */
package org.jeecg.modules.flow.callback.inbound.other;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.business.third.service.IMesStatusService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.inbound.mapper.SendMesDataVO;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 条料纵剪入库计算库区信息
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-16
 */
@Component
@Slf4j
@AllArgsConstructor
public class R12GetAreaBeforeBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsInReceiptTaskMaterialService iWmsInReceiptTaskMaterialService;
    private final IWmsInReceiptTaskService iWmsInReceiptTaskService;
    private final IWmsBasicWarehouseAreaService iWmsBasicWarehouseAreaService;
    private final IMesStatusService iMesStatusService;

    @Override
    public void actureRun() {
        //根据任务id创建入库单
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        String taskId = baseFlowDto.getInfoId();
        // 创建一个发送mes数据集合
        List<SendMesDataVO> collect = new ArrayList<>();
        // 根据任务ID查询入库任务物料集合
        List<WmsInReceiptTaskMaterial> list = iWmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskId)
        );
        if (CollectionUtil.isNotEmpty(list)) {
            //传入库策略计算出的库区
            for (WmsInReceiptTaskMaterial o : list) {
                SendMesDataVO vo = new SendMesDataVO();
                vo.setBatchId(o.getBatchCode());
                // 临时处理如果没有入库策略就传入默认值
                vo.setPosition("A");
                WmsBasicWarehouseArea area = iWmsBasicWarehouseAreaService.getById(o.getWmsBasicWarehouseAreaId());
                if (ObjectUtil.isNotNull(area)) {
                    if (StringUtils.isNotBlank(area.getCode())) {
                        String[] split = area.getCode().split("-");
                        vo.setPosition(split[split.length - 1]);
                    } else {
                        vo.setPosition("A");
                    }
                } else {
                    vo.setPosition("A");
                }
                collect.add(vo);
            }
        }
        iWmsInReceiptTaskService.update(new LambdaUpdateWrapper<WmsInReceiptTask>()
                .set(WmsInReceiptTask::getDocumentStatus, DocumentStatusEnum.PROCESS.getCode())
                .eq(WmsInReceiptTask::getId, taskId)
        );
        // 给MES回传库区信息
        try {
            boolean warehouse = iMesStatusService.warehouse(taskId, null, 3, collect);
            if (!warehouse) {
                baseFlowDto.setCurrentFailed(true);
                baseFlowDto.setResult(Result.error("通知MES库区失败"));
                return;
            }
        } catch (Exception e) {
            log.info("通知mes任务出现异常:", e);
            baseFlowDto.setCurrentFailed(true);
            baseFlowDto.setResult(Result.error("通知MES库区失败"));
            return;
        }
        baseFlowDto.setInfoId(taskId);
        baseFlowDto.setResult(Result.OK());
    }

}
